package heuristic;
import static skeleton.Board.*;

public class AdjStyleCounter {
	public int[] mine, ops;
	int liveFour, liveThree;
	int incx[] = {0, 1, 1, 1, 0, -1, -1, -1};
	int incy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
	

	private boolean outBound(int chessx, int chessy) {
		if (chessx<LEN && chessx>=0 && chessy<LEN && chessy >=0) return false;
		return true;
	}

	public void count(int[][] data, int myColor, int opColor, int x, int y) {
		mine = new int[6];
		ops = new int[6];
		int[] chessNum = new int[8];
		int[] len = new int[8];
		for (int i=0; i<8; ++i) {
			int empty = 0;
			int a, b, p = 1;
			while (true) {
				a = x + p * incx[i];
				b = y + p * incy[i];
				if (!outBound(a, b)) {
					if (data[a][b] == myColor) {
						++chessNum[i];
						empty = 0;
					}
					else if (data[a][b] == EMPTY) {
						++empty;
						if (empty == 2) {
							++len[i];
							for (int j=1; j<3; ++j) {
								a = x + (p + j) * incx[i];
								b = y + (p + j) * incy[i];
								if (!outBound(a, b) && data[a][b] != opColor)
									++len[i];
							}
							break;
						}
					}
					else break;
				}
				else break;
				++p;
				++len[i];
			}
		}
		int t;
		for (int i=0; i<4; ++i) {
			if (len[i] + len[i+4] < 4) continue;
			t = chessNum[i] + chessNum[i+4];
			if (t >= 5) ++mine[5];
			else ++mine[t];
		}
		for (int i=0; i<8; ++i) {
			int empty = 0;
			int a, b, p = 1;
			chessNum[i] = 0;
			len[i] = 0;
			while (true) {
				a = x + p * incx[i];
				b = y + p * incy[i];
				if (!outBound(a, b)) {
					if (data[a][b] == opColor) {
						++chessNum[i];
						empty = 0;
					}
					else if (data[a][b] == EMPTY) {
						++empty;
						if (empty == 2) {
							++len[i];
							for (int j=1; j<3; ++j) {
								a = x + (p + j) * incx[i];
								b = y + (p + j) * incy[i];
								if (!outBound(a, b) && data[a][b] != opColor)
									++len[i];
							}
							break;
						}
					}
					else break;
				}
				else break;
				++p;
				++len[i];
			}
		}
		for (int i=0; i<4; ++i) {
			if (len[i] + len[i+4] < 4) continue;
			t = chessNum[i] + chessNum[i+4];
			if (t >= 5) ++ops[5];
			else ++ops[t];
		}
	}
	
	public void count2(int[][] board, int x, int y, int myColor) {
		int[] adjsame = new int[8];
		int[] adjempty = new int[8];
		int[] jumpsame = new int[8];
		int[] jumpempty = new int[8];
		int[] jumpjumpsame = new int[8];

		for (int i=0; i<8; ++i) {
			adjsame[i] = adjempty[i] = jumpsame[i] = 0;
			jumpempty[i] = jumpjumpsame[i] = 0;
		}

		board[x][y] = myColor;

		int _x, _y;
		int initx[] = {x, x+1, x+1, x+1, x, x-1, x-1, x-1};
		int inity[] = {y-1, y-1, y, y+1, y+1, y+1, y, y-1};
		int incx[] = {0, 1, 1, 1, 0, -1, -1, -1};
		int incy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
		for (int i=0; i<8; ++i) {
			_x = initx[i]; _y=inity[i];
			for (;_x<LEN&&_x>=0&&_y<LEN&&_y>=0&&board[_x][_y]==myColor; 
				_x+=incx[i],_y+=incy[i],++adjsame[i]);
			for (;_x<LEN&&_x>=0&&_y<LEN&&_y>=0&&board[_x][_y]==EMPTY;
				_x+=incx[i],_y+=incy[i],++adjempty[i]);
			for (;_x<LEN&&_x>=0&&_y<LEN&&_y>=0&&board[_x][_y]==myColor;
				_x+=incx[i],_y+=incy[i],++jumpsame[i]);
			for (;_x<LEN&&_x>=0&&_y<LEN&&_y>=0&&board[_x][_y]==EMPTY;
				_x+=incx[i],_y+=incy[i],++jumpempty[i]);
			for (;_x<LEN&&_x>=0&&_y<LEN&&_y>=0&&board[_x][_y]==myColor;
				_x+=incx[i],_y+=incy[i],++jumpjumpsame[i]); 
		}
		
		board[x][y] = EMPTY;
		
		int fourCount = 0, threeCount = 0;
		
		for (int i=0; i<4; ++i) {
			if (adjsame[i] + adjsame[i+4] == 3) { //?0000?
				boolean isFour = false;
				if (adjempty[i] > 0 && adjempty[i+4] > 0)  //?+0000+?
						isFour = true;
				if (isFour)
					++fourCount;
			}
			else if (adjsame[i] + adjsame[i+4] == 2) { //?000?
				boolean isThree = false;
				//?++000+?
				if ((adjempty[i] > 2 || adjempty[i] == 2 && jumpsame[i] == 0) &&
						(adjempty[i+4]>1||adjempty[i+4]==1&&jumpsame[i+4]==0))
						isThree = true;
				//?+000++?
				if ((adjempty[i+4]>2||adjempty[i+4]==2&&jumpsame[i+4]==0)&&
						(adjempty[i]>1||adjempty[i]==1&&jumpsame[i]==0))
						isThree = true;
				if (isThree)
					++threeCount;
			}
			else if (adjsame[i] + adjsame[i+4] == 1) { //?00?
				if (adjempty[i]==1 && jumpsame[i]==1 && ////?+00+0+?
						(jumpempty[i]>1||jumpempty[i]==1&&jumpjumpsame[i]==0) &&
						(adjempty[i+4]>1||adjempty[i+4]==1&&jumpsame[i+4]==0))
						++threeCount;
			}
			else if (adjsame[i]+adjsame[i+4]==0) { //?0?
				if (adjempty[i]==1 && jumpsame[i]==2 && //?+00+0+?
						(jumpempty[i]>1 || jumpempty[i]==1&&jumpjumpsame[i]==0)&&
						(adjempty[i+4]>1||adjempty[i+4]==1&&jumpsame[i+4]==0))
						++threeCount;
				if (adjempty[i+4]==1 && jumpsame[i+4]==2 && //?+0+00+? 
						(jumpempty[i+4]>1||jumpempty[i+4]==1&&jumpjumpsame[i+4]==0)&&
						(adjempty[i]>1||adjempty[i]==1&&jumpsame[i]==0))
						++threeCount;
			}
		}
		
		liveFour = fourCount;
		liveThree = threeCount;
	}
}
